# Basic .bash_alias file for command line shortcuts
# Save this as ~/.bash_aliases
# On your ~/.bashrc add `. ~/.bash_aliases`
# Reboot or relogin
# To try without reboot (on current bash session), run `touch ~/.bash_aliases`

su_bin=doas
[ -n "$(command -v sudo)" ] && su_bin=sudo

## ---- Package Manager based... ---- ##
if [ -x "$(command -v apt)" ]; then
	# Debian based
	alias update-system="$su_bin apt update && $su_bin apt upgrade -y ; sync"
	alias install-package="$su_bin apt install -y "
	alias remove="$su_bin apt autoremove -y "
	alias search='apt search '
	alias clean-packages="$su_bin apt clean"
elif [ -x "$(command -v pacman)" ]; then
	# Arch linux based
	alias update-system="$su_bin pacman -Syu --noconfirm ; sync"
	alias update-mirrors="$su_bin "'reflector --verbose --latest 5 \
 --sort rate --save /etc/pacman.d/mirrorlist'
	alias install-package="$su_bin pacman --noconfirm -S "
	alias remove="$su_bin pacman --noconfirm -Rs "
	alias remove-orphans="pacman -Qtdq | $su_bin pacman -Rns -"
	alias search='pacman -Ss '
	alias clean-packages="$su_bin pacman -Scc"
	alias find-pacs='find /etc -regextype posix-extended \
-regex ".+\.pac(new|save)" 2> /dev/null'
elif [ -x "$(command -v xbps-install)" ]; then
	# Void Linux based
	alias update-system="$su_bin xbps-install -Syu ; sync"
	alias install-package="$su_bin xbps-install -y "
	alias remove="$su_bin xbps-remove -Ry "
	alias search='xbps-query -Rs '
	alias clean-packages="$su_bin xbps-remove -O"
elif [ -x "$(command -v zypper)" ]; then
	# Suse based
	alias update-system="$su_bin zypper refresh && $su_bin zypper refresh ; sync"
	alias install-package="$su_bin zypper install -y "
	alias remove="$su_bin zypper remove -y "
	alias search='zypper search '
	alias clean-packages="$su_bin zypper clean metadata && $su_bin zypper clean packages"
elif [ -x "$(command -v rpm)" ]; then
	# RPM based
	alias update-system="$su_bin yum -y update ; sync"
	alias install-package="$su_bin yum install -y "
	alias remove="$su_bin yum remove -y "
	alias search='yum search '
	alias clean-packages="$su_bin yum clean metadata && $su_bin yum clean packages"
elif [ -x "$(command -v guix)" ]; then
	# Guix/GuixSD based
	alias update-system='guix pull && guix package -u ; sync'
	alias install-package='guix install '
	alias remove='guix remove '
	alias search='guix search '
elif [ -x "$(command -v pkg)" ]; then
	# FreeBSD systems
	alias update-system="$su_bin pkg update && $su_bin pkg upgrade -y ; sync"
	alias install-package="$su_bin pkg install -y "
	alias remove="$su_bin pkg remove -y "
	alias remove-orphans="$su_bin pkg autoremove -y"
	alias search='pkg search '
	alias clean-packages="$su_bin pkg clean -y"
elif [ -x "$(command -v pkg_add)" ]; then
	# OpenBSD systems
	alias update-system="$su_bin pkg_add -u ; sync"
	alias install-package="$su_bin pkg_add "
	alias remove="$su_bin pkg_delete "
	alias remove-orphans='$su_bin pkg_delete -a'
	alias search='pkg_info -c -Q '
elif [ -x "$(command -v apk)" ]; then
	# Alpine Linux
	alias update-system="$su_bin apk update && $su_bin apk upgrade ; sync"
	alias install-package="$su_bin apk add "
	alias remove="$su_bin apk del "
	alias search='apk search '
	alias clean-packages="$su_bin apk cache clean"
fi


## ---- Service Related ---- ##
# usage:
#   handleService start|stop|restart|enable|disable <service name>
#   handleService list
handleService() {
	if [ -x "$(command -v systemctl)" ]; then # for SystemD
		service_list=$(systemctl list-unit-files -l)
		if [ ${1} == 'list' ]; then
			echo "$service_list"
		else
			if [[ `echo "$service_list" | grep "^${2}\.service"` != '' ]]; then
				$su_bin systemctl ${1} ${2}
			else
				echo "$2 service not found"
			fi
		fi
	# The "-f" check should be first for FreeBSD
	elif [ -f "/usr/sbin/service" ] || [[ "$(cat /proc/1/comm 2>/dev/null)" = "init" ]]; then # for SysVinit or "service" binary
		# to check if any of the commands fail and return a non-zero value for nonexisting service
		if [ "$(uname -s)" = "FreeBSD" ]; then
			service_list=$(service -l)
			echo "$service_list" | grep ${2}
		else
			service_list=$($su_bin service --status-all)
			$su_bin service ${2} status
		fi
		retcode="$?"
		if [ ${1} == 'list' ]; then
			echo "$service_list"
		else
			if [ "$retcode" = "0" ]; then
				$su_bin service ${2} ${1}
			else
				echo "$2 service not found"
			fi
		fi
	elif [ -x "$(command -v rcctl)" ]; then # for OpenBSD
		service_list=$(rcctl ls all)
		if [ ${1} == 'list' ]; then
			echo "$service_list"
		else
			if [[ `echo "$service_list" | grep "^${2}$"` != '' ]]; then
				$su_bin rcctl ${1} ${2}
			else
				echo "$2 service not found"
			fi
		fi
	elif [[ "$(cat /proc/1/comm 2>/dev/null)" = "runit" ]]; then # for runit
		enabled_dir='/etc/runit/runsvdir/current/'
		if [ -d '/etc/runit/sv/' ]; then # Artix
			service_dir='/etc/runit/sv/'
		else # Void Linux and possibly others
			service_dir='/etc/sv/'
		fi
		service_list=$(ls -1 ${service_dir})
		if [ ${1} == 'list' ]; then
			echo "${service_list}"
		else
			if [ `echo "${service_list}" | grep "^${2}$"` ]; then
				if [ ${1} == 'enable' ]; then
					$su_bin ln -s ${service_dir}${2} ${enabled_dir}${2}
				elif [ ${1} == 'disable' ]; then
					$su_bin rm ${enabled_dir}${2}
				else
					if [ -d ${enabled_dir}${2} ]; then
						$su_bin sv ${1} ${2}
					fi
				fi
			else
				echo "$2 service not found"
			fi
		fi
	fi
}
alias start-service='handleService start'
alias stop-service='handleService stop'
alias restart-service='handleService restart'
alias enable-service='handleService enable'
alias disable-service='handleService disable'
alias list-services='handleService list'
handleServer() {
	handleService ${1} apache
	handleService ${1} apache2
	handleService ${1} httpd
	handleService ${1} mariadb
	handleService ${1} mysqld
}
alias start-server='handleServer start'
alias stop-server='handleServer stop'
alias restart-server='handleServer restart'

# killall alternative for systems without it
if [ ! -x "$(command -v killall)" ]; then
	killall () {
		echo `pidof $1` && $su_bin kill -9 `pidof $1` || echo "$1 not running"
	}
fi


## ---- Git Related ---- ##
# Returns 0 if pwd is git root dir
check_git_root() {
	[ -d '.git' ] && return 0 || return 1
}
function _gtac {
	if check_git_root; then
		git add .
		git commit -m "$1"
	else
		echo 'Error: not git repo root'
		return 1
	fi
}
alias gtpull='git pull --ff-only'
alias gtpush='git push'
alias gts='git status'
alias gtd='git diff'
alias gtac='_gtac'
alias gtrevert='check_git_root && ( git reset && git checkout . && git clean -fdx ) || echo "Error: not git repo root"'
alias gtcl='git config --list'
alias gtc='git config'
alias gtl='git log'


## ---- Misc ---- ##
alias list='ls -hN --color=auto --group-directories-first'
alias update-grub="$su_bin grub-mkconfig -o /boot/grub/grub.cfg"
alias download-youtube='youtube-dl --write-auto-sub --embed-subs \
 -f "(mp4)[height<480]" '
alias list-groups='cut -d: -f1 /etc/group | sort'

if [ -x "$(command -v nvim)" ]; then
	alias v='nvim'
elif [ -x "$(command -v vim)" ]; then
	alias v='vim'
elif [ -x "$(command -v vi)" ]; then
	alias v='vi'
fi

alias show-metadata='exiftool '
alias strip-metadata='exiftool -all= -overwrite_original'

alias run-qemu="qemu-system-$(uname -m) -m 1024 -net nic -net user \
-soundhw all -machine accel=kvm -cdrom "

alias update-vpns="$su_bin python3 ~/.scripts/autovpngate.py"
alias ip-info='curl -s https://ifconfig.co/json || wget -qO- https://ifconfig.co/json || echo "failed!"'

alias cpu-usage="awk '{u=\$2+\$4; t=\$2+\$4+\$5; if (NR==1){u1=u; t1=t;} \
else print (\$2+\$4-u1) * 100 / (t-t1) \"%\"; }' \
<(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)"

internal_battery_usage() { for bn in /sys/class/power_supply/BAT*; do [ -d "${bn}" ] && echo "Battery ${bn:27:1}: $(cat ${bn}/capacity)% ($(cat ${bn}/status))"; done }
alias battery-usage='internal_battery_usage'

# Budgie related
alias restart-budgie='nohup budgie-panel --replace&'

# GNOME related
alias launch-dark='setsid env GTK_THEME=Adwaita:dark '
alias start-gnome-wayland='QT_QPA_PLATFORM=wayland XDG_SESSION_TYPE=wayland dbus-run-session gnome-session'
alias start-gnome-xorg='GDK_BACKEND=x11 gnome-session'

